﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SeniorCapstoneProject.ServiceReference3;

namespace SeniorCapstoneProject
{
    public partial class frmSettings : Form
    {
        int changed = 0;
        verifyUser uLogin = new verifyUser();
        frmMain mainForm = new frmMain();

        public frmSettings(verifyUser uLogin,frmMain mainForm)
        {
            InitializeComponent();
            this.uLogin = uLogin;
            this.mainForm = mainForm;
        }

        private void txtTemp_TextChanged(object sender, EventArgs e)
        {
            changed = 1;
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            txtTemp.Text = Properties.Settings.Default.Temp;
            txtDownload.Text = Properties.Settings.Default.ExtractLocation;
            changed = 0;
        }

        private void txtDownload_TextChanged(object sender, EventArgs e)
        {
            changed = 1;
        }

        private void btnBrowse1_Click(object sender, EventArgs e)
        {
            openFolderDialog(txtTemp.Name);
        }

        private void openFolderDialog(string lblName)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                if (txtTemp.Name == lblName)
                {
                    txtTemp.Text = fbd.SelectedPath;
                }
                else
                {
                    txtDownload.Text = fbd.SelectedPath;
                }
            }
        }

        private void btnBrowse2_Click(object sender, EventArgs e)
        {
            openFolderDialog(txtDownload.Name);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Temp = txtTemp.Text;
            Properties.Settings.Default.ExtractLocation = txtDownload.Text;
            Properties.Settings.Default.Save();
        }

        private void frmSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (changed == 1)
            {
                CustomMessage cm = new CustomMessage();
                cm.lblTitle.Text = "Changes made:";
                cm.lblMessage.Text = "Are you sure you want to cancel without saving changes?";
                cm.Text = "Confrim";
                if (cm.ShowDialog() == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            verifyUser tempUser = new verifyUser();
            tempUser.Password = txtCurrentPassword.Text;
            if (tempUser.Password != uLogin.Password)
            {
                mainForm.customMessage("Error", "Invalid Password", "Your current password doesn't match existing password.", "Ok", null);
            }
            else if (txtNewPassword.Text != txtRetypePassword.Text || txtNewPassword.Text.Length < 6)
            {
                mainForm.customMessage("Error", "Password missmatch", "The new passwords don't match eachother or is smaller than 6 characters long.", "Ok", null);
            }
            else
            {
                tempUser.Password = txtNewPassword.Text;
                TransferFilesClient tfc = new TransferFilesClient();
                int i = tfc.ChangePassword(uLogin.Username, uLogin.Password, uLogin.Username, tempUser.Password);
                if (i == 99)
                {
                    mainForm.customMessage("Error!!!", "UHOH", "Something went wrong on the server side. Please contact support.", "OK", null);
                }
                lblSuccess.Text = "Password changed.";
                txtCurrentPassword.Text = null;
                txtNewPassword.Text = null;
                txtRetypePassword.Text = null;
                uLogin.Password = tempUser.Password;
                mainForm.uLogin = uLogin;
            }
        }
    }
}
